﻿using System.Reactive.Linq;
using System.Reflection;

using Microsoft.Extensions.DependencyInjection;

using TimeSpan.Views.Windows;

namespace TimeSpan.Views;

public static class ViewsExtension {

    public static IServiceCollection AddAllViews(this IServiceCollection services) {
        services.AddSingleton<MainWindow>();
        services.AddSingleton<MainWindowViewModel>();

        services.AddTransient<TaskRecordWindow>();
        services.AddSingleton<TaskRecordWindowViewModel>();

        var allPageTypes = Assembly.GetExecutingAssembly().GetTypes()
            .Where(x => $"{x.Namespace}".StartsWith("TimeSpan.Views.Pages")).ToObservable();

        allPageTypes.Where(x => x.Name.EndsWith("ViewModel"))
            .Do(x => services.AddSingleton(x)).Subscribe();

        allPageTypes.Where(x => x.Name.EndsWith("Page"))
            .Do(x => services.AddSingleton(x)).Subscribe();

        return services;
    }

}